package com.zdzx.controller.admin.content;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdzx.common.exception.BaseException;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.CourseTestsMapper;
import com.zdzx.model.service.content.vo.CourseTests;
import com.zdzx.utils.PoiUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "试题")
@RestController
@RequestMapping("/admin")
public class EnterpriseTestsController {
	
	
	@Autowired
	private CourseTestsMapper courseTestsMapper;
	
	
	@ApiOperation(value = "获取列表", notes = "") 
	@GetMapping("/course/tests/{courseId}") 
	public Result<Map<String,List<CourseTests>>> getTests(@PathVariable("courseId")String courseId){
		QueryWrapper<CourseTests> queryWrapper=new QueryWrapper<CourseTests>();
		queryWrapper.eq("course_id", courseId);
		List<CourseTests> list = courseTestsMapper.selectList(queryWrapper);
		Map<String,List<CourseTests>> map=new HashMap<String, List<CourseTests>>();
		List<CourseTests> d=new ArrayList<CourseTests>();
		List<CourseTests> s=new ArrayList<CourseTests>();
		for (CourseTests courseTests : list) {
			if("0".equals(courseTests.getType())) {
				d.add(courseTests);
			}else if("1".equals(courseTests.getType())) {
				s.add(courseTests);
			}
		}
		map.put("d", d);
		map.put("s", s);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null,map);
	}

	@ApiOperation(value = "删除", notes = "") 
	@DeleteMapping("/course/tests/{courseId}/{type}") 
	public Result<Integer> del(@PathVariable("courseId")String courseId){
		QueryWrapper<CourseTests> wrapper=new QueryWrapper<CourseTests>();
		wrapper.eq("course_id", courseId);
		int rows = courseTestsMapper.delete(wrapper);
		if(rows>0){
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null,null);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),ResultCode.RESULT_ERROR.getCode(),null);
		}
	}
	
	
	
	@ApiOperation(value = "导入课程", notes = "") 
	@PostMapping("/course/tests/{courseId}/{type}") 
	@Transactional(rollbackFor = Exception.class)
	public Result<Integer> save(@PathVariable("courseId")String courseId,
			@PathVariable("type")String type,@RequestParam("file") MultipartFile file){
		if(StringUtils.isEmpty(courseId)) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "导入课程ID不正确", null);
		}
		try {
		Map<String, List<String>> importXLS = PoiUtil.ImportXLS(file.getInputStream(), null);
		if(importXLS.get("Sheet1")==null||importXLS.get("Sheet1").size()<=1) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"格式不正确",null);
		}
		if(importXLS.get("Sheet1").size() % 2 != 0) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"格式不正确",null);
		}
		QueryWrapper<CourseTests> wrapper=new QueryWrapper<CourseTests>();
		wrapper.eq("course_id", courseId);
		wrapper.eq("`type`", type);
		courseTestsMapper.delete(wrapper);
		List<String> list = importXLS.get("Sheet1");
		for(int i=0;i<list.size();i++) {
			String[] split = list.get(i).split("zzzzz");
			CourseTests entity=new CourseTests();
			entity.setCourseId(courseId);
			entity.setAnswer(split[1]);
			entity.setContent(split[0]);
			entity.setType(type);
			i++;
			if(!list.get(i).contains("zzzzz")) {
				throw new BaseException();
			}
			String[] split2 = list.get(i).split("zzzzz");
			
			entity.setOptContent(JSON.toJSONString(split2));
			courseTestsMapper.insert(entity);
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),null,null);
		}catch (BaseException e) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),"格式不正确",null);
		}
		catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(),ResultCode.RESULT_ERROR.getCode(),null);
		}
	}
}
